package com.qianhui.servlet;

import com.qianhui.common.GlobalConfig;
import com.qianhui.dao.OrderDAO;
import com.qianhui.pojo.Buyer;
import com.qianhui.pojo.Order;
import org.json.JSONObject;
import weixin.popular.api.PayMchAPI;
import weixin.popular.bean.paymch.Unifiedorder;
import weixin.popular.bean.paymch.UnifiedorderResult;
import weixin.popular.util.PayUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Servlet implementation class getWechatOrderInfo
 */
@WebServlet("/getWechatOrderInfo.do")
public class getWechatOrderInfo extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public getWechatOrderInfo()
    {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String currentUrl = request.getParameter("currentUrl");
        String order_id = request.getParameter("order_id");
        Buyer buyerSession = (Buyer) request.getSession().getAttribute("buyerSession");

        OrderDAO orderDAO = new OrderDAO();
        Order order = orderDAO.findById(order_id);

        Map<String, Object> json = new HashMap<String, Object>();

        Unifiedorder unifiedorder = new Unifiedorder();
        unifiedorder.setAppid(GlobalConfig.appID);
        unifiedorder.setMch_id(GlobalConfig.mch_id);
        unifiedorder.setNonce_str(UUID.randomUUID().toString().toString().replace("-", ""));
        unifiedorder.setOpenid(buyerSession.getBuyerWechat());
        unifiedorder.setBody(order.getOrderId());
        unifiedorder.setOut_trade_no(order.getOrderId());
        unifiedorder.setTotal_fee(String.valueOf((int) (order.getOrderTotalPrice() * 100)));// 单位分
        unifiedorder.setSpbill_create_ip(request.getRemoteAddr());// IP
        unifiedorder.setNotify_url(GlobalConfig.receive_notify_url);
        unifiedorder.setTrade_type("JSAPI");// JSAPI，NATIVE，APP，WAP
        // 统一下单，生成预支付订单
        UnifiedorderResult unifiedorderResult = PayMchAPI.payUnifiedorder(unifiedorder, GlobalConfig.API_KEY);

        String json_param = PayUtil.generateMchPayJsRequestJson(unifiedorderResult.getPrepay_id(), GlobalConfig.appID, GlobalConfig.API_KEY);

        System.out.println("json_param:" + json_param);
        json.put("param", json_param);
        JSONObject jo = new JSONObject(json);
        out.write(jo.toString());
        out.flush();
        out.close();
    }

}
